/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupof.projectphotos.messagesys;

import edu.upf.grupof.projectphotos.databases.iPhoto;
import edu.upf.grupof.projectphotos.messageobj.GenericMessage;
import edu.upf.grupof.projectphotos.messageobj.SearchProfile;
import edu.upf.grupof.projectphotos.messageobj.UserContent;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferNegotiator;
import org.jivesoftware.smackx.muc.MultiUserChat;

/**
 *
 * @author fredi
 */
public class InstantMessage implements iInstantMessage {

    private XMPPConnection con = null;
    //public XMPPConnection con2 = null;
    private MultiUserChat chat = null;
    private boolean connect = false;
    private boolean dconnect = true;
    private PacketFilter filter = null;
    private String server = "talk.google.com";
    private int port = 5222;
    private String mail = "gmail.com";
    private String resource = "";
    private static InstantMessage instantMessage = null;
    private Roster roster = null;
    private List<iPhoto> photoResults;
    private String path;

    private InstantMessage() {
    }

    public synchronized static InstantMessage getInstance() {
        if (instantMessage == null) {
            instantMessage = new InstantMessage();
        }
        return instantMessage;
    }
    
    public void getResults(List<iPhoto> photoResults){
        this.photoResults=null;
        this.photoResults=photoResults;
    }
    
    public List<iPhoto> setResults(){
        return photoResults;
    }

    //Login to GoogleTalk
    public boolean login(String name, String pass, String resource) throws XMPPException {
        this.resource=resource;
        
        //Configuring connection to googleTalk
        ConnectionConfiguration config = new ConnectionConfiguration(server, port, mail);
        config.setSASLAuthenticationEnabled(true);
        config.setReconnectionAllowed(true);
        con = new XMPPConnection(config);
        
        //Connecting googleTalk
        con.connect();
        con.login(name, pass, resource);
        
        //Filter for the communication. The connection will just send
        //messages of types "Message"
        filter = new PacketTypeFilter(Message.class);
        con.createPacketCollector(filter);
        //con2=con;
        
        //Initializing chat
        chat = new MultiUserChat(con, name + "'s Chat");
        
        //Getting Roster from googleTalk
        roster = con.getRoster();
        
        //Changing connection constants
        connect = true;
        dconnect = false;
        
        //Adding the listener for reciving messages

        listener();
        receiver();

        return connect;
    }

    public XMPPConnection getCon(){
        return con;
    }
    
    //Logout
    public boolean logout() {
        if (con != null) {
            con.disconnect();
            con = null;
            
            //Changing connection constants
            dconnect = true;
            connect = false;
        }
        return dconnect;
    }

    //Sender
    public boolean sender(Object obj) throws XMPPException{
        
        GenericMessage gm= (GenericMessage) obj;
        
        //Creating the XMPP message
        Message msg = new Message();
        String packetid = "message";

        //Adding destination, id and the GenericMessage into the XMPP Message
        msg.setTo(gm.getDest() + "/" + resource);
        msg.setPacketID(packetid);
        msg.setProperty(packetid, gm);
        //Sending the message
        chat.sendMessage(msg);

        //If no exceptions come out...
        return true;
    }
    
    //Listener
    public void listener() {
        PacketListener list = new Listener(instantMessage);
        con.addPacketListener(list, filter);
    }
    
    public void getPath(String path){
        this.path=path;
    }
    
    //Files receiver
    public void receiver() {
        
        FileReceiver receiver = new FileReceiver(instantMessage, path);
        final FileTransferManager manager = new FileTransferManager(con);
        FileTransferNegotiator.setServiceEnabled(con,true);
        manager.addFileTransferListener(receiver);
    }

    //Search
    public boolean search(List<String> tags, String type) throws XMPPException {
        boolean done = false;

        //Searching for contacts to send the message
        for (Iterator<RosterEntry> i = roster.getEntries().iterator(); i.hasNext();) {
            RosterEntry entry = (RosterEntry) i.next();
            
            //Creating the SearchProfile with origen, destination and the SearchProfile
            SearchProfile prof = new SearchProfile(con.getUser(), entry.getUser(), tags, type);

            done = instantMessage.sender(prof);
        }
        return done;
    }
    
    public boolean getContent(String user, String name, String path) throws XMPPException {
        boolean done;
        
        UserContent uc= new UserContent (con.getUser(), user, name, path);
        done=instantMessage.sender(uc);
        
        return done;
        
    }
}
